package junit.admin;

import com.jfinal.aop.Inject;
import io.jboot.test.MockMvcResult;
import io.jboot.utils.StrUtil;
import io.jpress.module.article.model.ArticleCategory;
import io.jpress.module.article.service.ArticleCategoryService;
import junit.base.AdminBaseTester;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class AdminArticleCategoryTester extends AdminBaseTester {


    @Inject
    private ArticleCategoryService articleCategoryService;

    @Test
    public void startArticleCategoryTestCases() {

        //登录
        doLogin();

        //新增文章分类
        testAddArticleCategory();

        //修改文章分类
        testUpdateArticleCategory();

        //删除文章分类
        testDeleteArticleCategory();

        //退出
        doLogout();

    }


    /**
     * 测试新增文章分类
     */
    public void testAddArticleCategory() {
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setTitle("test title");

        Map<String, Object> paras = buildParas(articleCategory, "category");
        MockMvcResult mvcResult = postWithCsrfToken("/admin/article/doCategorySave", paras);
        Assert.assertTrue(mvcResult.getContent().contains("固定链接不能为空"));


        articleCategory.setSlug(StrUtil.uuid());
        paras = buildParas(articleCategory, "category");
        mvcResult = postWithCsrfToken("/admin/article/doCategorySave", paras);

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long articleCategoryId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("articleCategoryId", articleCategoryId);

        mvcResult.printResult();
    }


    /**
     * 测试修改文章分类
     */
    public void testUpdateArticleCategory() {

        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setTitle("test title");
        articleCategory.setSlug("test-new-slug");
        articleCategory.setId((Long) getContainerData("articleCategoryId"));

        Map<String, Object> paras = buildParas(articleCategory, "category");
        MockMvcResult mvcResult = postWithCsrfToken("/admin/article/doCategorySave", paras);
        Long articleCategoryId = mvcResult.getContentAsJSONObject().getLong("id");

        final ArticleCategory byId = articleCategoryService.findById(articleCategoryId);
        Assert.assertEquals(byId.getContent(), articleCategory.getContent());
    }


    /**
     * 测试删除文章文章分类
     */
    public void testDeleteArticleCategory() {
        Long articleCategoryId = (Long) getContainerData("articleCategoryId");
        MockMvcResult mvcResult = getWithCsrfToken("/admin/article/doCategoryDel/" + articleCategoryId);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
    }

}
